package com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionZW

import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragment
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkDialog.ShowDialogTools
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.AibkSingleQuesEvaluationActivity
import android.content.Context
import android.os.Handler
import android.util.Log
import android.view.View
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragmentPagerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.QuesEvaluationDetail
import com.lancoo.znbkxx.teacher.utils.WebStyleUtils
import com.lancoo.znbkxx.teacher.utils.WebStyleUtils.returnImageUrlsFromHtml
import com.lancoo.znbkxx.teacher.widget.KWebView
import kotlinx.android.synthetic.main.aibk_fragment_model_zw.view.*
import kotlinx.android.synthetic.main.aibk_include_article_view.view.*
import kotlinx.android.synthetic.main.aibk_include_child_answer_title_infor.view.*
import kotlinx.android.synthetic.main.aibk_include_loading_refresh.view.*
import kotlinx.android.synthetic.main.aibk_include_parent_audioplayer_a.view.*
import kotlinx.android.synthetic.main.aibk_include_ques_answer_view.view.*
import kotlinx.android.synthetic.main.aibk_include_ques_assessment.view.*
import kotlinx.android.synthetic.main.aibk_include_ques_web_ask_view.view.*
import kotlinx.android.synthetic.main.aibk_include_question_information_view.view.*
import java.util.*


class QuesEvaluationFragmentZw(
        val activity: AppCompatActivity,
        val quesId: String,
        val startIndex:Int
) : KBaseFragment() {

    private var rootView: View? = null
    var quesEvaluationDetail: QuesEvaluationDetail? = null
    private var isGetQuesDetail = false
    private var fragmentList: MutableList<Fragment>? = ArrayList()

    override fun createLayout(): Int {
        return R.layout.aibk_fragment_model_splitlayout
    }

    override fun initView(view: View?) {
        rootView = view
        rootView!!.RlGifLoading.visibility = View.VISIBLE
        rootView!!.RlAudioPlayer.visibility = View.GONE
        rootView!!.RlArticleView.visibility = View.GONE
        rootView!!.RlChildAnswerTitle.visibility = View.GONE
    }

    private fun showQuestionView(quesEvaluationDetail: QuesEvaluationDetail) {

        //是否显示来源
        var resource = quesEvaluationDetail.Resource
        if (resource.isEmpty()) {
            rootView!!.IncludeQuesInformationView.visibility = View.GONE
        } else {
            rootView!!.IncludeQuesInformationView.visibility = View.VISIBLE
            rootView!!.IncludeQuesInformationView.TvSourceFrom.text = "来源: $resource"
            //隐藏试题准确率
            rootView!!.IncludeQuesInformationView.TvScoreRate.visibility = View.GONE
        }

        //隐藏单题评估系数
        rootView!!.RlQuesAssessment.visibility = View.GONE

        //加载题干内容
        val genreID = quesEvaluationDetail.GenreID
        var ask = quesEvaluationDetail.ChildList[0].ChildAsk
        if(genreID == 35){
            val paperZwIntroduction = resources.getString(R.string.paper_zw_introduction)
            ask = "<b>$paperZwIntroduction</b><br>$ask"
        }
        ask = ask.replace("—".toRegex(), "_")
        ask = ask.replace("______________________".toRegex(), "_")
        WebStyleUtils.showWebViewCssStyle(getActivity(), rootView!!.WvAsk, ask, false)
        rootView!!.WvAsk.addJavascriptInterface(
                JSInterface(activity, returnImageUrlsFromHtml(ask)), "Android"
        )
        rootView!!.WvAsk.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                rootView!!.RlWebLoading.visibility = View.GONE
                WebStyleUtils.setWebImageClick(view, "Android")
            }
        }

        rootView!!.WvAsk.setOnScrollChangeListener(object : KWebView.OnScrollChangeListener {
            override fun onPageEnd(l: Int, t: Int, oldl: Int, oldt: Int) {
                Log.e("KWvA", "已经到达地端")
            }

            override fun onPageTop(l: Int, t: Int, oldl: Int, oldt: Int) {
                Log.e("KWvA", "已经到达顶端")
            }

            override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
                Log.e("KWvA", "=======oldt========$oldt")
            }
        })

        //加载作答区域
        fragmentList!!.clear()
        (fragmentList as ArrayList<Fragment>).add(
                QuesEvaluationFragmentParentZw(activity,quesEvaluationDetail,0)
        )

        val mAdapter = KBaseFragmentPagerAdapter(childFragmentManager, fragmentList as ArrayList<Fragment>)
        rootView!!.VpAnswer.adapter = mAdapter
    }

    //JS点击事件处理
    inner class JSInterface(val context: Context, var imageUrls: Array<String>?) {

        @JavascriptInterface
        fun clickFunction(position: String) {
            return
        }

        //java回调js代码，不要忘了@JavascriptInterface这个注解，不然点击事件不起作用
        @JavascriptInterface
        fun openImage(img: String, pos: Int) {
            val paths = ArrayList<String>()
            paths.add(img)
//            Log.e("openImage", "$pos========点击了图片=========$img")
            ShowDialogTools.showBigImageDialog(activity, img)
        }
    }

    override fun setMenuVisibility(menuVisible: Boolean) {
        super.setMenuVisibility(menuVisible)
        if(!isGetQuesDetail&&menuVisible) {

            Handler().postDelayed({

                    val parentActivity: AibkSingleQuesEvaluationActivity? = getActivity() as AibkSingleQuesEvaluationActivity?
                    parentActivity?.getSingleQuesEvaluation(quesId, startIndex)
                    parentActivity?.setDetailListener(object : AibkSingleQuesEvaluationActivity.OnDetailListener {

                        override fun getQuesDetailSuccess(quesEvaluationDetailX: QuesEvaluationDetail) {
                            quesEvaluationDetail = quesEvaluationDetailX
                            isGetQuesDetail = true
                            rootView!!.RlGifLoading.visibility = View.GONE
                            showQuestionView(quesEvaluationDetail!!)
                        }

                        override fun getQuesDetailFaile() {

                        }

                    })

            }, 500)
        }
    }

}